﻿using System;

namespace Cpu6502.Opcodes
{
    class BRK : Opcode
    {
        public BRK()
        {
            this.CpuCycles = 7;
            this.AddressType = CPU.AddressType.Implied;
        }

        public override void Execute()
        {
            CPU.PC++;
            // Push return address onto stack.
            CPU.Stack.Push((byte)((CPU.PC >> 8) & 0xff));
            CPU.Stack.Push((byte)(CPU.PC & 0xff));

            // Set Break interrupt flag
            CPU.Registers.P.B = true;

            // Push CPU State flags onto the stack
            CPU.Stack.Push(CPU.Registers.P.Value);

            // Set Interrupt flag
            CPU.Registers.P.I = true;

            // JMP to interrupt code
            CPU.PC = (CPU.Memory[0xFFFe] | (CPU.Memory[0xFFFF] << 8));
        }
    }
}
